<?php

namespace App\Modules\Shop;

use App\Modules\Good\Models\Good;
use App\Modules\Package\Models\GoodPackage;
use Illuminate\Console\Scheduling\Schedule as BaseSchedule;

class Schedule
{
    public static function invoke(BaseSchedule $schedule)
    {
        $schedule->call([self::class, 'refreshCount'])->daily()->name('刷新商品数量');
    }

    public static function refreshCount()
    {
        Shop::query()
            ->where('status', Shop::STATUS_OPEN)
            ->each(function ($item) {
                $packageCount = GoodPackage::query()->where('shop_id', $item->id)->count();
                $goodsCount = Good::query()->where('shop_id', $item->id)->count();
                $item->goods_number = $packageCount + $goodsCount;
                $item->save();
            });
    }
}
